/**
 * @file tiny_timer.h
 * @author 张恒宣 
 * @LastEditTime 2025/4/06
 * 
 * MIT License
 *
 * Copyright (c) 2025 张恒宣 luckkeymail@163.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __TINY_TIMER_H__
#define __TINY_TIMER_H__

/*********************
 *      头文件
 *********************/

#include "tiny_def.h"
#include "tiny_list.h"

/**********************
 *      类型定义
 **********************/

typedef void (*tiny_timer_callback_t)(void *);

typedef enum
{
		TIMER_OWNER_NONE = 0,
    TIMER_OWNER_THREAD,
    TIMER_OWNER_USER
} TIMER_OWNER;

typedef enum
{
    TIMER_ONCE,
    TIMER_PERIODIC
} TIMER_TYPE;

typedef enum
{
    TIMER_RUNNING,
    TIMER_STOPPED
} TIMER_STATUS;

typedef struct
{
	TIMER_OWNER timer_owner;
	tiny_uint64_t timeout;
	tiny_uint32_t interval;
	tiny_list_node_t list_link;
	tiny_timer_callback_t callback;
	void *arg;
	TIMER_TYPE type;
	TIMER_STATUS status;
	tiny_uint8_t no_used[2];
} tiny_timer_t;

/**********************
 *      函数声明
 **********************/

void tiny_timer_init(void);
void tiny_timer_obj_init(tiny_timer_t *timer, TIMER_OWNER owner, TIMER_TYPE type, tiny_uint32_t interval, tiny_timer_callback_t callback, void *arg);
tiny_timer_t *tiny_timer_create(TIMER_TYPE type, tiny_uint32_t interval, tiny_timer_callback_t callback, void *arg);
void tiny_timer_delete(tiny_timer_t *timer);
void tiny_timer_set_interval(tiny_timer_t *timer, tiny_uint32_t interval);
void tiny_timer_start(tiny_timer_t *timer);
void tiny_timer_stop(tiny_timer_t *timer);
TIMER_OWNER tiny_check_timers(void);


#endif //! __TINY_TIMER_H__
